/**
 * @ProjectName: config-console-app
 * @Description: ChongQing Financial Assets Exchange.
 */
package com.cqfae.deploycentral.app.web.controller;

import com.cqfae.deploycentral.app.config.SSOInfo;
import com.cqfae.deploycentral.app.services.CommonControllerService;
import com.cqfae.deploycentral.app.web.response.WebResponse;
import com.cqfae.deploycentral.app.web.vo.SsoVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>登陆信息接口</p>
 *
 * @author YANGHENG940 2017年4月26日 上午10:19:57
 * @version V1.0
 * @date 2017年4月26日 上午10:19:57
 */
@RestController
@RequestMapping("/deploy-central/login")
public class LoginController implements CommonControllerService<Object> {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SSOInfo ssoInfo;


    /**
     * @param @param  request
     * @param @return
     * @return WebResponse
     * @throws
     * @Title: getUserToken
     * @user YANGHENG940
     */
    @RequestMapping(value = "/getUserToken")
    public WebResponse getUserToken(@CookieValue(value = "token", defaultValue = "")
                                            String tokenId) {
        logger.info(" ====console getUserToken ===={}", tokenId);
        return getWebResponse(new SsoVo(ssoInfo.getGotoUrl(), tokenId, ssoInfo.getIsOpen(), "123"));
    }

    @RequestMapping(value = "/logout")
    public WebResponse logout(HttpServletRequest request, HttpServletResponse response) {
        //清空cookies内容
        Cookie cookie = new Cookie("token",null);
        cookie.setPath("/");
        cookie.setDomain("paic.com.cn");
        cookie.setMaxAge(0);
        response.addCookie(cookie);

        return getWebResponse(new SsoVo(ssoInfo.getGotoUrl(), "", ssoInfo.getIsOpen(), "123"));
    }

    @RequestMapping(value = "/get-user-name")
    public WebResponse getUserName() {
        return getWebResponse("123");
    }

    @RequestMapping(value = "/get-user-menu")
    public WebResponse getUserMenuInfo() {
        return getWebResponse("123");
    }

    @Override
    public WebResponse getWebResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        if (obj != null) {
            webResponse.setData(obj);
        }
        return webResponse;
    }

}
